
package testPlayer;

import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import player.*;
import junit.framework.TestCase;
import org.junit.Test;

public class PlayerIOTest extends TestCase{
		
		@Test
		public void testprintPlayerArr_1() throws Exception {
			setOutput();
			
			PlayerIO io = new PlayerIO();
			Player[] pArr = new Player[2];
			for (int i=0; i<2; i++) {
				pArr[i] = new Player();
				pArr[i].setPlayerID((i+1));
			}
			
			io.printPlayerArr(pArr);
			
			String newLine = System.getProperty("line.separator");
			if (newLine == null) newLine = "\n";
			
			String expected = "\nThere are 2 players joining the game now." + "\n" +
			"Below are their own information: " + "\n" + 
			"--------------------------------------" + "\n" + "Player 1" + "\tRole: unknown" + "\n" +
			"Player 1 right now has 3 SE Points" + "\n" + 
			"Player 1 now has 1 magic card" + "\n" + "escapeHospital" + "\n" + "\n" +  
			"\n" + "End of Player Information!!!!" + "\n" + "***************************************" +
			"\n" + "Player 2" + "\tRole: unknown" + "\n" +
			"Player 2 right now has 3 SE Points" + "\n" + 
			"Player 2 now has 1 magic card" + "\n" + "escapeHospital" + "\n" + "\n" +  
			"\n" + "End of Player Information!!!!" + "\n" + "***************************************" +"\n";
			
			//get result
			String actualResult = this.getOutput();
			//System.out.print(expectedOut);
			System.out.print(actualResult);
			
			assertEquals(expected, actualResult);
		}
		
		@Test
		public void testValidPlayerNo_1(){
			PlayerIO pIO = new PlayerIO();
			boolean actual = pIO.validPlayerNo(0);
			boolean expected = false;
			assertEquals(expected, actual);
		}
		
		@Test
		public void testValidPlayerNo_2(){
			PlayerIO pIO = new PlayerIO();
			boolean actual = pIO.validPlayerNo(2);
			boolean expected = true;
			assertEquals(expected, actual);
		}
		
		@Test
		public void testPromptRollForOrder_1() throws Exception{
			setOutput();
			String separator = System.getProperty("line.separator");
			PlayerIO pIO = new PlayerIO();
			pIO.promptRollForOrder(null, 1, 2);
			String expected = "============================================================" +separator;
			expected += "Now, roll a die to determine your order in playing the game.\n";
			//get result
			String actual = this.getOutput();
			//System.out.print(expectedOut);
			System.out.print(actual);
			
			assertEquals(expected+separator, actual);
		}
		PrintStream oldPrintStream;
		ByteArrayOutputStream bos;  

		private void setOutput() throws Exception
		{
			oldPrintStream = System.out;
			bos = new ByteArrayOutputStream();  
			System.setOut(new PrintStream(bos)); 
		}
		
		private String getOutput() //throws Exception
		{
			System.setOut(oldPrintStream);
			return bos.toString();
		}
}
